#!/bin/bash

export PATH=/usr/local/bin:/usr/bin:$PATH

cd /home/alert/docker-compose/backend/web

cat /home/alert/script/password.txt | sudo -S /usr/local/bin/docker-compose down

container_name=$(cat /home/alert/script/password.txt | sudo -S docker ps -a --format "{{.ID}} {{.Names}}" | awk '$2 == "iot-dh-web-backend" {print $1}')
echo "本次使用容器:${container_name}"
if [ -n "${container_name}" ]
then
  echo "删除旧容器 CONTAINER ID: ${container_name}"
  cat /home/alert/script/password.txt | sudo -S docker stop ${container_name}
  cat /home/alert/script/password.txt | sudo -S docker rm  ${container_name}
fi



image_name="60.174.236.111:8090/dh/iot-dh-web-backend"
echo "本次使用镜像:${image_name}"
#删除旧镜像
exist_image=$(cat /home/alert/script/password.txt | sudo -S docker images | grep ${image_name}  | awk '{print $3}')
if [ -n "${exist_image}" ]
then
  echo "删除旧镜像 IMAGE ID: ${exist_image}"
  cat /home/alert/script/password.txt | sudo -S docker rmi ${exist_image}
fi

cat /home/alert/script/password.txt | sudo -S /usr/local/bin/docker-compose pull
cat /home/alert/script/password.txt | sudo -S /usr/local/bin/docker-compose up -d